//
//  segmentDownloadManage.m
//  LocalHttpServerExample
//
//  Created by Lin Eidan on 13-9-6.
//  Copyright (c) 2013年 taomee. All rights reserved.
//

#import "SegmentDownloadManage.h"
#import "M3U8DownloadConfig.h"
#import "DownloadGetData.h"
#import "AppDelegate.h"
#import "Common.h"
#import "VideoObj.h"

@interface SegmentDownloadManage ()

@property (strong, nonatomic) VideoObj *videoObj;

@end

@implementation SegmentDownloadManage

-(id)initWithDownloadURL:(NSString *)url andFileName:(NSString *)fileName andSaveToPath:(NSString *)saveToFolder andFileDur:(float )fileDur andVideoObj:(VideoObj *)obj{
    self = [super init];
    if (self)
    {
        self.downloadUrl = [NSString stringWithString:[url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
        self.fileName = fileName;
        self.saveToPath=saveToFolder;
        self.segmentDur=fileDur;
        self.timeProgess=0.0;
        self.videoObj = obj;
    }
    return  self;
}

-(void)startSegmentDownload{
    NSString *filePath=[NSString stringWithFormat:@"%@/%@",self.saveToPath,self.fileName];
    if (![LocalFileManage isExistFile:filePath]) {
        if (isiOS7) {
            [[DownloadGetData sharedInstance] startTaskBackgroundDownload:self.downloadUrl andFileFolderPath:self.saveToPath andFileName:self.fileName andAllowResume:NO andStartRightNow:YES andResumeData:nil andProgress:^(NSURLSessionDownloadTask *task, float percent) {
                
            } andCompletion:^(NSURLResponse *response, long long contentLength, BOOL isSuccess) {
                if (isSuccess) {
                    self.timeProgess=self.segmentDur;
                    if ([self.delegate respondsToSelector:@selector(segmentDownloadManageFinish:andSegmentDur:andIsSuccess:)]) {
                        [self.delegate segmentDownloadManageFinish:self andSegmentDur:self.segmentDur andIsSuccess:YES];
                    }
                }else{
                    if ([self.delegate respondsToSelector:@selector(segmentDownloadManageFinish:andSegmentDur:andIsSuccess:)]) {
                        [self.delegate segmentDownloadManageFinish:self andSegmentDur:self.segmentDur andIsSuccess:NO];
                    }
                }
            }];
            
        }else{
            self.videoObj.requestOperaiton = [[DownloadGetData sharedInstance] startDownload:self.downloadUrl andFilePath:filePath andAllowResume:NO andProgress:^(AFDownloadRequestOperation *operation, float percent, long long totalBytesReadForFile, long long totalBytesExpectedToReadForFile) {
                
            } andCompletion:^(AFHTTPRequestOperation *operation, long long contentLength, BOOL isSuccess) {
                if (isSuccess) {
                    self.timeProgess=self.segmentDur;
                    if ([self.delegate respondsToSelector:@selector(segmentDownloadManageFinish:andSegmentDur:andIsSuccess:)]) {
                        [self.delegate segmentDownloadManageFinish:self andSegmentDur:self.timeProgess andIsSuccess:YES];
                    }
                }else{
                    if ([self.delegate respondsToSelector:@selector(segmentDownloadManageFinish:andSegmentDur:andIsSuccess:)]) {
                        [self.delegate segmentDownloadManageFinish:self andSegmentDur:self.segmentDur andIsSuccess:NO];
                    }
                }
            }];
            self.videoObj.requestTempFilePath = self.videoObj.requestOperaiton.tempPath;  //这样才能删除缓存文件
        }
        
    }else{
        self.timeProgess=self.segmentDur;
        if ([self.delegate respondsToSelector:@selector(segmentDownloadManageFinish:andSegmentDur:andIsSuccess:)]) {
            [self.delegate segmentDownloadManageFinish:self andSegmentDur:self.timeProgess andIsSuccess:YES];
        }
    }

}

@end
